//随机文本
function randomText() {
  const text = "abcdefghijklmnopqrstuvwsyz0123456789";
  const letter = text[Math.floor(Math.random() * text.length)];
  return letter;
}

function rain() {
  const cloud = document.querySelector(".cloud");
  const e = document.createElement("div");

  //随机向左移动距离（雨宽）
  const left = Math.floor(Math.random() * 310);
  //随机字体大小
  const size = Math.random() * 1.5;
  //随机落体速度
  const duration = Math.random() * 1;

  e.classList.add("text");
  cloud.appendChild(e);

  e.innerText = randomText();
  e.style.left = left + "px";
  e.style.fontSize = 0.5 + size + "em";
  //不同的落体速度
  e.style.animationDuration = 1 + duration + "s";

  //定时移除
  setTimeout(() => {
    cloud.removeChild(e);
  }, 3000);
}

// 循环时执行
let intervalId = setInterval(() => {
  rain();
}, 50);

// 控制执行的开关函数
function start() {
  if (!intervalId) {
    intervalId = setInterval(function () {
      rain();
    }, 50);
  }
}
function stop() {
  if (intervalId) {
    clearInterval(intervalId);
    intervalId = null;
  }
}

// 滚动条控制执行的开关
isExcute = true; // 执行状态标志
$(function () {
  window.addEventListener("scroll", function () {
    if (document.documentElement.scrollTop < 400) {
      if (isExcute != true) {
        isExcute = true;
        start();
      }
    } else {
      if (isExcute == true) {
        isExcute = false;
        stop();
      }
    }
  });
});

document.querySelector("a.button").addEventListener("hover");
